package okhttp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.*;

import java.io.File;
import java.io.IOException;

/**
 * Created by lynn on 2016/6/25.
 */
public class ReqJson {
	static String svr = "http://127.0.0.1:8080";
	static String serverBaseUrl = "http://192.168.43.82:8080";

	static MediaType json = MediaType.parse("application/json; charset=utf-8");

	public static void main(String[] args) throws IOException {
		classes();
	}

	static void classes() {
		String s = "{\"objectlist\":[{\"id\":18,\"clsname\":\"c1\",\"school\":\"s1\",\"validation\":\"05069H468K\",\"description\":\"dddddd\"},{\"id\":16,\"clsname\":\"asd\",\"school\":\"sad\",\"validation\":\"w8H7VRz4c2\",\"description\":\"adsa\"}],\"flag\":\"success\"}";
		JSONObject j = JSONObject.parseObject(s);
		JSONArray jclasslist = j.getJSONArray("objectlist");
		System.out.println(jclasslist.size());
		for (int i = 0; i < jclasslist.size(); i++) {
			JSONObject jclass = jclasslist.getJSONObject(i);
			System.out.println(jclass);
			int id = jclass.getIntValue("id");

			System.out.println(id);
		}
	}

	static void reg() throws IOException {
		Request req = signUpRequest("zt1", "111", "tower1");
		Response res = client.newCall(req).execute();
		System.out.println(res.code());
	}

	static OkHttpClient client = new OkHttpClient();

	static Request signUpRequest(String uid, String pwd, String name) {
		JSONObject j = new JSONObject().fluentPut("uid", uid).fluentPut("password", pwd).fluentPut("nickname", name);

		RequestBody requestBody = RequestBody.create(PostType_JSON, j.toString());

		Request request = new Request.Builder().url(serverBaseUrl + "/register").post(requestBody).build();
		return request;
	}

	static int signIn(String uid, String pwd) {
		Request request = loginRequest(uid, pwd);
		OkHttpClient client = new OkHttpClient();
		try {
			Response response = client.newCall(request).execute();
			if (response.code() == 200) {
				JSONObject jres = JSONObject.parseObject(response.body().string());
				if ("success".equals(jres.getString("flag"))) {
//                    Env.setSignedInUser(jres.getString("userid"), uid);
					return 1;
				} else {
					return 2;
				}
			} else {
				return 3;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 4;
		}
	}


	static Request loginRequest(String uid, String pwd) {
		JSONObject j = new JSONObject().fluentPut("uid", uid).fluentPut("password", pwd);

		RequestBody requestBody = RequestBody.create(PostType_JSON, j.toString());

		Request request = new Request.Builder().url(serverBaseUrl + "/login").post(requestBody).build();
		return request;
	}

	static MediaType PostType_JSON = MediaType.parse("application/json; charset=utf-8");

	static void pre() throws Exception {
		String text = "hello-upload";
		String filePath = "E:\\0.jpg";
		RequestBody requestBody = new MultipartBuilder()
				.type(MultipartBuilder.FORM)
				.addFormDataPart("media", filePath, RequestBody.create(MediaType.parse("image/jpeg"), new File(filePath)))
				.addFormDataPart("text", text)

				.build();
		System.out.println(requestBody.contentType());
		Request request = new Request.Builder().url(svr + "/pub").post(requestBody).build();
		System.out.println(request.headers().toMultimap());
//        System.exit(0);
		Response res = new OkHttpClient().newCall(request).execute();
		System.out.println(res.code());
		System.out.println(res.body().string());
	}

	static void g() throws IOException {
		OkHttpClient client = new OkHttpClient();
		JSONObject j = new JSONObject().fluentPut("id", 233).fluentPut("name", "杜小月").fluentPut("birthday", "19760101");
		RequestBody requestBody = RequestBody.create(json, j.toString());
		Request request = new Request.Builder().url(svr + "/stu").post(requestBody).build();
		Response response = client.newCall(request).execute();
		System.out.println(response.code());
		System.out.println(response.body().string());
	}

}
